#ifndef UTIL_UTILNET_H_
#define UTIL_UTILNET_H_

#include <stdio.h>
#include <string>
#include <mutex>

namespace Util
{
	class UtilNet
	{
	public:
		static int DownloadFile(const char *fileName, const char *localPath, const char *downloadURL);
		static int UploadStream(unsigned char *in, int inLen, FILE *fp, const char* uploadURL, int &statusCode);
		static int FtpUpload(const char *localPath, const char *ftpPath, const char *user, const char *pwd);

	private:
		static bool _init;
	};

	class TransFile
	{
	public:
		TransFile();

		int start_download(const char* url, const char* localPath);
		int start_upload(const char* url, const char* localPath, const char* user = NULL, const char* pwd = NULL);
		void stop();

	private:
		static size_t CallBack_write(char *buffer, size_t size, size_t nitems, void* outstream);
		size_t write(char *buffer, size_t size, size_t nitems);

		static size_t CallBack_read(char *buffer, size_t size, size_t nitems, void *instream);
		size_t read(char *buffer, size_t size, size_t nitems);

		static int CallBack_process(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
		virtual int process(double dltotal, double dlnow, double ultotal, double ulnow);

	private:
		FILE*		_fp;
		int			_trans_direct;
		bool		_run;
		long		_file_len;
		std::string _file_name;

		static std::mutex	_mtx;
		static bool			_init;
	};
}

#endif UTIL_UTILNET_H_